package com.yh.service.impl;

import com.yh.entity.Product;
import com.yh.entity.ProductSku;
import com.yh.mapper.ProductMapper;
import com.yh.mapper.ProductSkuMapper;
import com.yh.service.IOrderClientService;
import com.yh.service.IProductService;
import io.seata.spring.annotation.GlobalTransactional;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

/**
 * @author yh
 */
@Service
@AllArgsConstructor
public class ProductServiceImpl implements IProductService {
    private final ProductMapper productMapper;
    private final ProductSkuMapper productSkuMapper;
    private final IOrderClientService orderClientService;

    @Override
    @GlobalTransactional
    public void buyProducts(Long productId, Long skuId, Integer nums) {
        ProductSku productSku = productSkuMapper.selectById(skuId);
        BigDecimal money = productSku.getPrice().multiply(new BigDecimal(nums));

        Product product = productMapper.selectById(productId);
        product.setStock(product.getStock() - nums);
        productMapper.updateById(product);

        orderClientService.genOrder(productId,skuId,nums,money,1);
    }
}
